package org.taobao.ibatiside.ui.action;

import org.eclipse.jface.action.IMenuManager;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.viewers.ISelectionProvider;
import org.eclipse.jface.viewers.SelectionChangedEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.ui.IActionBars;
import org.eclipse.ui.navigator.CommonActionProvider;
import org.eclipse.ui.navigator.CommonViewer;
import org.eclipse.ui.navigator.ICommonActionExtensionSite;
import org.taobao.ibatiside.core.IbatisConstant;
import org.taobao.ibatiside.core.filemodel.IbatisConfigFile;
import org.taobao.ibatiside.core.filemodel.IbatisMapperFile;
import org.taobao.ibatiside.core.filemodel.IbatisProject;
import org.taobao.ibatiside.core.filemodel.IbatisStatement;
import org.taobao.ibatiside.ui.Activator;

public class OverviewActionProvider extends CommonActionProvider {
	private static final String GROUP_EDIT = "group.edit";
	private static final String GROUP_BASIC = "group.basic";
	private ISelectionProvider selectionProvider;
	OverviewAction newMapper = new OverviewNewMapperAction();
	OverviewAction edit = new OverviewEditStatementAction();
	OverviewAction openInWizard = new OverviewOpenInWizardAction();
	OverviewAction delete = new OverviewDeleteStmtAction();
	OverviewNewConfigAction newConfig = new OverviewNewConfigAction();
	OverviewDeleteAction deleteMapper = new OverviewDeleteMapperAction();
	ImageDescriptor v2Img;
	ImageDescriptor v3Img;
	private CommonViewer viewer;
	public OverviewActionProvider() {
		super();
	}
	@Override
	public void init(ICommonActionExtensionSite aSite) {
		super.init(aSite);
		this.selectionProvider = aSite.getViewSite().getSelectionProvider();
		viewer = (CommonViewer) aSite.getStructuredViewer();
		newMapper.initialize(viewer);
		edit.initialize(viewer);
		openInWizard.initialize(viewer);
		delete.initialize(viewer);
		newConfig.initialize(viewer);
		deleteMapper.initialize(viewer);
		v2Img = Activator.getIcon("eview16/batisv2.png");
		v3Img = Activator.getIcon("eview16/batisv3.png");
	}
	@Override
	public void fillActionBars(IActionBars actionBars) {
		super.fillActionBars(actionBars);
	}
	@Override
	public void fillContextMenu(IMenuManager menu) {
		
		if (getContext().getSelection().isEmpty()
				|| !(getContext().getSelection() instanceof StructuredSelection)) {
			return;
		}
		StructuredSelection view_selection = (StructuredSelection) getContext()
				.getSelection();
		SelectionChangedEvent event = new SelectionChangedEvent(
				this.selectionProvider, getContext().getSelection());
		Object object = view_selection.getFirstElement();
		if (object instanceof IbatisConfigFile){
			newMapper.selectionChanged(event);
			menu.insertAfter(GROUP_BASIC, newMapper);
		}else if(object instanceof IbatisMapperFile){
			openInWizard.selectionChanged(event);
			menu.insertAfter(GROUP_BASIC, openInWizard);
			deleteMapper.selectionChanged(event);
			menu.insertAfter(GROUP_EDIT, deleteMapper);
		}else if (object instanceof IbatisStatement){
			edit.selectionChanged(event);
			delete.selectionChanged(event);
			menu.insertAfter(GROUP_BASIC, edit);
			menu.insertAfter(GROUP_BASIC, delete);
		}else if (object instanceof IbatisProject){
			int ver = ((IbatisProject)object).getVersion();
			if (ver == IbatisConstant.VERSION_TWO){
				newConfig.setImageDescriptor(v2Img);
			}else if (ver == IbatisConstant.VERSION_MYBATIS){
				newConfig.setImageDescriptor(v3Img);
			}
			newConfig.selectionChanged(event);
			menu.insertAfter(GROUP_BASIC, newConfig);
		}
	}

}
